﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Table_pet.background.Classes;

namespace Table_pet.background
{

    internal class DB_api
    {
        private static readonly DbContextOptions<DBcontext> contextOptions = new DbContextOptionsBuilder<DBcontext>()
               .UseMySQL("Server=localhost;Database=petDB;User=root;Password=zamm030130")
               .Options;
        public static List<Memorandum> readAllMemorandum()
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var q = context.Memorandum;
                    return q.ToList();
                };
            }
            catch
            {
                return null;
            }
        }
        public static List<ClockModel> readAllClock()
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var q = context.Clock;
                    return q.ToList();
                };
            }
            catch
            {
                return null;
            }
        }
        public static long AddMemorandumDate(Memorandum memorandum)
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    context.Memorandum.Add(memorandum);
                    context.SaveChanges();
                    return memorandum.Id;
                };
            } catch { 
                return 0;
            }
        }

        public static long AddClock(ClockModel model)
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    context.Clock.Add(model);
                    context.SaveChanges();
                    return model.Id;
                };
            }
            catch
            {
                return 0;
            }
        }
        public static Memorandum deleteMemorandumById(long id)
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var mem= context.Memorandum.FirstOrDefault(x => x.Id == id);
                    if (mem != null)
                    {
                        context.Memorandum.Remove(mem);
                        context.SaveChanges();
                    }
                    return mem;
                };
            }   
            catch
            {
                return null;
            }
        }
        public static ClockModel deleteClockById(long id) {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var clock = context.Clock.FirstOrDefault(x => x.Id == id);
                    if (clock != null)
                    {
                        context.Clock.Remove(clock);
                        context.SaveChanges();
                    }
                    return clock;
                };
            }
            catch
            {
                return null;
            }
        }
        public static Memorandum searchMemorandumById(long id)
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var mem = context.Memorandum.FirstOrDefault(x => x.Id == id);
                    return mem;
                };
            }
            catch
            {
                return null;
            }
        }
        public static ClockModel searchClockById(long id)
        {
            try
            {
                using (var context = new DBcontext(contextOptions))
                {
                    var clock = context.Clock.FirstOrDefault(x => x.Id == id);
                    return clock;
                };
            }
            catch
            {
                return null;
            }
        }

    }
}
